///******************** (C) COPYRIGHT 2019 STMicroelectronics ********************
//* File Name          : gatt_db.c
//* Author             : BlueNRG-LP database file for Sensor demo
//* Version            : V1.0.0
//* Date               : 02-April-2019
//* Description        : Functions to build GATT DB and handle GATT events.
//********************************************************************************
//* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
//* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
//* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
//* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
//* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
//* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
//*******************************************************************************/


//#include <stdio.h>
//#include <string.h>
//#include <stdlib.h>
//#include "ble_const.h" 
//#include "bluenrg_lp_stack.h"
//#include "clock.h" 
//#include "gp_timer.h" 
//#include "gatt_db.h"
//#include "osal.h"
//#include "SensorDemo_config.h"
//#include "gatt_profile.h"
//#include "gap_profile.h"
//#include "OTA_btl.h" 
//#include "global.h"

//#include "bluenrg_lp_evb_config.h" 

//#ifndef DEBUG
//#define DEBUG 0
//#endif

//#if DEBUG
//#include <stdio.h>
//#define PRINTF(...) printf(__VA_ARGS__)
//#else
//#define PRINTF(...)
//#endif
//#define INVALID_HANDLE                                (0xFF)
//#define ENV_SENS_SERVICE_UUID   0xfb,0x34,0x9b,0x5f,0x80,0x00,0x00,0x80,0x00,0x10,0x00,0x00,0x00,0xf0,0x00,0x00
//#define TEMP_CHAR_UUID          0xfb,0x34,0x9b,0x5f,0x80,0x00,0x00,0x80,0x00,0x10,0x00,0x00,0x00,0xf0,0x00,0x01
//#define PRESS_CHAR_UUID         0xfb,0x34,0x9b,0x5f,0x80,0x00,0x00,0x80,0x00,0x10,0x00,0x00,0x00,0xf0,0x01,0x01

//uint16_t envSensServHandle, tempCharHandle, pressCharHandle;

//extern uint16_t connection_handle;
//extern BOOL sensor_board;



///* Client Configuration Characteristics Descriptor Definition: free fall characteristic  */
//BLE_GATT_SRV_CCCD_DECLARE(cgmwrite,
//                     NUM_LINKS,
//                     BLE_GATT_SRV_CCCD_PERM_DEFAULT,
//                     BLE_GATT_SRV_OP_MODIFIED_EVT_ENABLE_FLAG);

///* Client Configuration Characteristics Descriptor Definition: acceleration characteristic*/
//BLE_GATT_SRV_CCCD_DECLARE(cgmread,
//                     NUM_LINKS,
//                     BLE_GATT_SRV_CCCD_PERM_DEFAULT,
//                     BLE_GATT_SRV_OP_MODIFIED_EVT_ENABLE_FLAG);




//static ble_gatt_chr_def_t env_chars[] = {
//    {/* Temperature Characteristic */
//        .properties = BLE_GATT_SRV_CHAR_PROP_NOTIFY|BLE_GATT_SRV_CHAR_PROP_WRITE_NO_RESP|BLE_GATT_SRV_CHAR_PROP_WRITE,
//        .permissions = BLE_GATT_SRV_PERM_NONE,
//        .min_key_size = BLE_GATT_SRV_MAX_ENCRY_KEY_SIZE,
//        .uuid = BLE_UUID_INIT_128(TEMP_CHAR_UUID),
//        .descrs = {
//            .descrs_p = &BLE_GATT_SRV_CCCD_DEF_NAME(cgmwrite),
//            .descr_count = 1,
//        },
//    },
//    {/* Pressure Characteristic */
//        .properties = BLE_GATT_SRV_CHAR_PROP_NOTIFY|BLE_GATT_SRV_CHAR_PROP_WRITE_NO_RESP|BLE_GATT_SRV_CHAR_PROP_WRITE,
//        .permissions = BLE_GATT_SRV_PERM_NONE,
//        .min_key_size = BLE_GATT_SRV_MAX_ENCRY_KEY_SIZE,
//        .uuid = BLE_UUID_INIT_128(PRESS_CHAR_UUID),
//        .descrs = {
//            .descrs_p = &BLE_GATT_SRV_CCCD_DEF_NAME(cgmread),
//            .descr_count = 1,
//        },
//    },
//};

//static ble_gatt_srv_def_t env_service = {
//   .type = BLE_GATT_SRV_PRIMARY_SRV_TYPE,
//   .uuid = BLE_UUID_INIT_128(ENV_SENS_SERVICE_UUID),
//   .chrs = {
//       .chrs_p = env_chars,
//       .chr_count = 2U,
//   },
//};


//tBleStatus Add_CGM_Sensor_Service(void)
//{
//  tBleStatus ret;
//  
//  ret = aci_gatt_srv_add_service(&env_service);
//  if (ret != BLE_STATUS_SUCCESS)
//  {
//    goto fail;
//  }
//  
//  envSensServHandle = aci_gatt_srv_get_service_handle(&env_service);
//  tempCharHandle = aci_gatt_srv_get_char_decl_handle(&env_chars[0]);
//  pressCharHandle = aci_gatt_srv_get_char_decl_handle(&env_chars[1]);
//  
//  return BLE_STATUS_SUCCESS; 
//  
//fail:
//  PRINTF("Error while adding ENV_SENS service.\n");
//  return BLE_STATUS_ERROR;
//}




//tBleStatus Temp_Update(int16_t temp)
//{  
//  tBleStatus ret;

//  ret = aci_gatt_srv_notify(connection_handle, tempCharHandle + 1, 0, 2, (uint8 *)&temp);
//  if (ret != BLE_STATUS_SUCCESS){
//    PRINTF("Error while updating TEMP characteristic: 0x%02X\n",ret);
//    return BLE_STATUS_ERROR ;
//  }
//  
//  return BLE_STATUS_SUCCESS;
//}


//void aci_gatt_srv_write_event(uint16_t Connection_Handle, uint8_t Resp_Needed, uint16_t Attribute_Handle, uint16_t Data_Length, uint8_t Data[])
//{
//		static uint8_t u8_count = 0 ;
//    uint8_t att_error = BLE_ATT_ERR_NONE;

//  OTA_Write_Request_CB(Connection_Handle, Attribute_Handle, Data_Length, Data); 

//    if (Resp_Needed == 1U)
//    {
//        aci_gatt_srv_resp(Connection_Handle, 0, att_error, 0, NULL);
//    }
//}

//void aci_gatt_srv_read_event(uint16_t Connection_Handle, uint16_t Attribute_Handle, uint16_t Data_Offset)
//{
//    uint8_t att_err;
//    uint8_t buff[6], *data_p;
//    uint16_t data_len;
//    int16_t temp_val;
//    int32_t press_val;
//    float fdata;
//#ifdef SENSOR_PRESSURE_TEMPERATURE_EMULATION
//    uint16_t udata;
//#endif

//    att_err = BLE_ATT_ERR_NONE;
//    if(Attribute_Handle == tempCharHandle + 1)
//    {
//        fdata = 27 + ((uint64_t)rand()*15)/RAND_MAX;
//			
//				data_len = 2;
//        temp_val = (int16_t)(fdata * 10);
//        data_p = (uint8_t *)&temp_val;
//    }
//    else if(Attribute_Handle == pressCharHandle + 1)
//    {
//        fdata = 1000 + ((uint64_t)rand()*1000)/RAND_MAX;
//        data_len = 3;
//        press_val = (int32_t)(fdata * 100);
//        data_p = (uint8_t *)&press_val;
//    }
//    

//    OTA_Read_Char(Connection_Handle, Attribute_Handle,Data_Offset);

//		 PRINTF("data_p: %d\n",*data_p);
//    aci_gatt_srv_resp(Connection_Handle, Attribute_Handle, att_err, data_len, data_p);
//}

